/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlet;

import IBO.IClientParticulier;
import IBO.IClientSociete;
import IBO.ITechnicien;
import IUSECASE.IUC_Factory;
import boerrors.ClientInexistantException;
import java.io.*;
import java.net.*;

import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author Administrateur
 */
public class Log extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        PrintWriter page = response.getWriter();
    }
    
    
    


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
        doPost(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");


        
        try {
            response.setContentType("text/html");
            
            //récupération du matricule de la page login
            String matricule = request.getParameter("matricule");
            String password = request.getParameter("password");
            //demande la liste des techniciens
            IUC_Factory factory = null;
            factory = (IUC_Factory) Naming.lookup("rmi://localhost:1099/factory");
            Vector<ITechnicien> techniciens = factory.getGestionEmployes().listerTechniciens();
            //request.setAttribute("techniciens", techniciens);         
            RequestDispatcher disp = this.getServletContext().getRequestDispatcher("/index.jsp");
            if(matricule.compareToIgnoreCase("")==0){

                  this.gotoPage("/loginError.jsp",request,response);

            }
            else{
                ITechnicien tech = null;
                for(int i=0;i<techniciens.size();i++){
                    if(Integer.parseInt(matricule) == techniciens.get(i).getMatricule()&& (techniciens.get(i).getPassword().compareTo(password)==0) )tech=techniciens.get(i);
                }
                if (tech!=null){                    
                    //chargement des clients des interventions
                        Vector <IClientSociete> listeClientSociete = new Vector<IClientSociete>();
                        Vector <IClientParticulier> listeParticulier = new Vector<IClientParticulier>();
                            for(int i=0;i<tech.getListeIInterventionsToday().size();i++){
                                int id = tech.getListeIInterventionsToday().get(i).getIClient().getIdClient();
                                Vector <IClientSociete> clientS = factory.getGestionClients().rechercherClientSociete_NumClient(id);
                                if (clientS.size() > 0) {                      
                                    if (!listeClientSociete.contains(clientS.get(0))) {
                                        listeClientSociete.add(clientS.get(0));
                                    }                                        
                                } else {
                                    Vector<IClientParticulier> clientP = factory.getGestionClients().rechercherClientParticulier_NumClient(id);                        
                                    if (! (listeParticulier.contains(clientP.get(0))) ) {
                                        listeParticulier.add(clientP.get(0));
                                    }
                                }
                            }
                     request.getSession().setAttribute("technicien", tech);
                     request.getSession().setAttribute("listeClientSociete", listeClientSociete);
                     request.getSession().setAttribute("listeParticulier", listeParticulier);
                     
                     
                    this.gotoPage("/TechnicienInfo.jsp",request,response);


                }
                else{
                      this.gotoPage("/loginError.jsp",request,response);

                }
            }
        } catch (ClientInexistantException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NotBoundException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(Log.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
    * Returns a short description of the servlet.
    */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
    
    private void gotoPage(String adress, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher =
		getServletContext().getRequestDispatcher(adress);
		dispatcher.forward(request,response);
	}

}
